#微视短视频
import selenium
from selenium import webdriver
import pathlib
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\Administrator\Desktop\视频发布"
# 视频描述
describe = "AI·热带亚马逊·奇幻星空 #视觉震撼 #旅行 #AI #星空 #大自然"
time.sleep(10)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:5003")
driver = webdriver.Chrome(options=options)

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if (".mp4" in str(i)):
        path_mp4 = str(i);
        break;

if (path_mp4 != ""):
    print("检查到视频路径：" + path_mp4)
else:
    print("未检查到视频路径，程序终止！")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if (".png" in str(i) or ".jpg" in str(i)):
        path_cover = str(i);
        break;

if (path_cover != ""):
    print("检查到封面路径：" + path_cover)
else:
    print("未检查到封面路径，程序终止！")
    exit()


def publish_weishi():
    '''
     作用：发布微视视频
    '''

    # 进入微视创作者页面，并上传视频
    driver.get("https://media.weishi.qq.com/")
    time.sleep(2)
    driver.find_element(By.XPATH,'//input[@type="file"]').send_keys(path_mp4)

    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element(By.XPATH,'//video')
            break;
        except Exception as e:
            print("视频还在上传中···")

    print("视频已上传完成！")

    # 添加封面
    driver.find_element(By.XPATH,'//div[text()="设置封面"]').click()
    time.sleep(1)
    # driver.find_element(By.XPATH,'//div[text()="图片上传"]').click()
    # time.sleep(1)
    # driver.find_element(By.XPATH,'//input[@type="file"]').send_keys(path_cover)
    time.sleep(1)
    driver.find_element(By.XPATH,'//div[text()="确定"]').click()

    time.sleep(1)
    # 输入视频描述
    driver.find_element(By.XPATH,'//textarea').send_keys(describe)

    # 人工进行检查并发布
    time.sleep(3)
    # 点击发布
    driver.find_element(By.XPATH,'//span[text()="发 布"]').click()


# 开始执行微视视频发布
publish_weishi()